package com.member.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.member.common.enums.EnableStatusEnum;
import com.member.common.enums.OrderDeleteEnum;
import com.member.common.utils.DateUtils;
import com.member.common.utils.bean.BeanUtils;
import com.member.shop.domain.SysBizGoods;
import com.member.shop.domain.SysBizMaterialCategory;
import com.member.shop.domain.SysBizShopCategory;
import com.member.shop.mapper.SysBizShopCategoryMapper;
import com.member.shop.service.ISysBizGoodsService;
import com.member.shop.service.ISysBizShopCategoryService;
import com.member.shop.vo.CategoryVo;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 商品分类Service业务层处理
 *
 * @author lixinchen
 * @date 2024-12-28
 */
@Service
public class SysBizShopCategoryServiceImpl extends ServiceImpl<SysBizShopCategoryMapper, SysBizShopCategory> implements ISysBizShopCategoryService {
    @Resource
    private SysBizShopCategoryMapper sysBizShopCategoryMapper;
    @Resource
    private ISysBizGoodsService sysBizGoodsService;

    /**
     * 查询商品分类
     *
     * @param id 商品分类主键
     * @return 商品分类
     */
    @Override
    public SysBizShopCategory selectSysBizShopCategoryById(Long id) {
        return sysBizShopCategoryMapper.selectById(id);
    }

    /**
     * 查询商品分类列表
     *
     * @param queryWrapper 商品分类
     * @return 商品分类
     */
    @Override
    public List<SysBizShopCategory> selectSysBizShopCategoryList(LambdaQueryWrapper<SysBizShopCategory> queryWrapper) {
        return sysBizShopCategoryMapper.selectList(queryWrapper);
    }

    @Override
    public List<SysBizShopCategory> selectSysBizShopCategoryList(SysBizShopCategory sysBizShopCategory) {
        return sysBizShopCategoryMapper.selectSysBizShopCategoryList(sysBizShopCategory);
    }

    /**
     * 新增商品分类
     *
     * @param sysBizShopCategory 商品分类
     * @return 结果
     */
    @Override
    public int insertSysBizShopCategory(SysBizShopCategory sysBizShopCategory) {
        sysBizShopCategory.setCreateTime(DateUtils.getNowDate());
        return sysBizShopCategoryMapper.insert(sysBizShopCategory);
    }

    /**
     * 修改商品分类
     *
     * @param sysBizShopCategory 商品分类
     * @return 结果
     */
    @Override
    public int updateSysBizShopCategory(SysBizShopCategory sysBizShopCategory) {
        sysBizShopCategory.setUpdateTime(DateUtils.getNowDate());
        return sysBizShopCategoryMapper.updateById(sysBizShopCategory);
    }

    /**
     * 批量删除商品分类
     *
     * @param ids 需要删除的商品分类主键
     * @return 结果
     */
    @Override
    public int deleteSysBizShopCategoryByIds(Long[] ids) {
        return sysBizShopCategoryMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除商品分类信息
     *
     * @param id 商品分类主键
     * @return 结果
     */
    @Override
    public int deleteSysBizShopCategoryById(Long id) {
        SysBizShopCategory sysBizShopCategory = new SysBizShopCategory();
        sysBizShopCategory.setId(id);
        sysBizShopCategory.setDeleted((long) OrderDeleteEnum.DELETE.getKey());
        return sysBizShopCategoryMapper.updateById(sysBizShopCategory);
    }

    /**
     * 是否存在目录子节点
     *
     * @param id 目录ID
     * @return 结果
     */
    @Override
    public boolean hasChildById(Long id) {
        int result = sysBizShopCategoryMapper.hasChildById(id);
        return result > 0;
    }

    @Override
    public List<CategoryVo> queryFirstCategory(Long id, Long tenantId) {
        List<CategoryVo> result = new ArrayList<CategoryVo>();
        LambdaQueryWrapper<SysBizShopCategory> sysBizShopCategoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysBizShopCategoryLambdaQueryWrapper.eq(id == null, SysBizShopCategory::getParentId, 0L);
        sysBizShopCategoryLambdaQueryWrapper.eq(id != null, SysBizShopCategory::getParentId, id);

        sysBizShopCategoryLambdaQueryWrapper.eq(SysBizShopCategory::getTenantId, tenantId);
        sysBizShopCategoryLambdaQueryWrapper.eq(SysBizShopCategory::getDeleted, OrderDeleteEnum.NOT_DELETE.getKey());
        sysBizShopCategoryLambdaQueryWrapper.eq(SysBizShopCategory::getDisplayed, EnableStatusEnum.ENABLE.getKey());
        sysBizShopCategoryLambdaQueryWrapper.orderByDesc(SysBizShopCategory::getOrderNum);
        sysBizShopCategoryLambdaQueryWrapper.orderByDesc(SysBizShopCategory::getId);
        List<SysBizShopCategory> sysBizShopCategoryList = sysBizShopCategoryMapper.selectList(sysBizShopCategoryLambdaQueryWrapper);
        if (CollectionUtils.isEmpty(sysBizShopCategoryList)) {
            return result;
        }
        for (SysBizShopCategory sysBizShopCategory : sysBizShopCategoryList) {
            CategoryVo categoryVo = new CategoryVo();
            BeanUtils.copyProperties(sysBizShopCategory, categoryVo);
            // 查询跳转二级分类还是跳转商品页
            if (id == null) {
                categoryVo.setType(0);
                Long count = sysBizShopCategoryMapper.selectCount(new LambdaQueryWrapper<>(SysBizShopCategory.class).eq(SysBizShopCategory::getParentId, sysBizShopCategory.getId()).eq(SysBizShopCategory::getDeleted, OrderDeleteEnum.NOT_DELETE.getKey()));
                if (count > 0) {
                    categoryVo.setType(1);
                }
            }
            result.add(categoryVo);
        }
        return result;
    }
}
